package cn.com.backstage.controller;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.ibm.icu.util.Calendar;

import cn.com.api.entity.*;
import cn.com.api.entity.ApiT_city;
import cn.com.api.entity.ApiT_member;
import cn.com.api.entity.ApiT_member_pay_records;
import cn.com.api.entity.ApiT_shipper_account;
import cn.com.api.entity.ApiT_shipping;
import cn.com.api.entity.ApiT_stats;
import cn.com.api.service.ApiOrderService;
import cn.com.api.service.ApiT_brandService;
import cn.com.api.service.ApiT_memberService;
import cn.com.api.service.ApiT_shipper_accountService;
import cn.com.api.service.ApiT_shippingService;
import cn.com.api.service.ApiT_statsService;
import cn.com.api.service.StartOnLoadService;
import cn.com.backstage.controller.base.T_brandBaseController;
import cn.com.backstage.controller.base.T_memberBaseController;
import cn.com.util.BaseObj;
import cn.com.util.DateUtil;
import cn.com.util.ImageUtils;
import cn.com.util.Tools;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE + "/")
public class T_brandController extends T_brandBaseController {

	private static Logger logger = Logger.getLogger(T_brandController.class);

	@Autowired
	private ApiT_brandService apiT_brandService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "brand-list")
	public ModelAndView brand_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_brand_list.jsp");
		List<ApiT_brand> t_brands = apiT_brandService.findT_brands();
		if (null != t_brands && t_brands.size() > 0) {
			mv.addObject("t_brands", t_brands);
		}
		return mv;
	}

	/**
	 * 新增修改界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "brand-add-pre")
	public ModelAndView brand_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/brand_add.jsp");
		String brand_id = request.getParameter("brand_id");
		if (null != brand_id && !"".equals(brand_id)) {
			ApiT_brand apiT_brand = apiT_brandService.findT_brandById(Integer.valueOf(brand_id));
			mv.addObject("brand", apiT_brand);
		}
		return mv;
	}

	/**
	 * 新增品牌
	 * 
	 * @return
	 */
	@Transactional
	@RequestMapping(value = "brand-add")
	public void brand_add(@ModelAttribute ApiT_brand apiT_brand, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		BaseObj baseObj = new BaseObj();

		try {
			System.out.println(apiT_brand.getBrand_id());
			if (null != apiT_brand.getBrand_id()&&!"".equals(apiT_brand.getBrand_id())) {// 更新
				apiT_brand.setLast_modify(DateUtil.nowTimeToString());
				apiT_brandService.updateT_brand(apiT_brand);
			} else {// 添加
				apiT_brand.setDisabled(1);
				apiT_brand.setLast_modify(DateUtil.nowTimeToString());
				apiT_brandService.insertT_brand(apiT_brand);
			}
			baseObj.handleSuccess();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			baseObj.handleException(e);
		}
		JsonUtil.printJson(response, JsonUtil.object2json(baseObj));
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "brand-delete")
	public void brand_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_brandService.deleteT_brand(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 * 品牌logo上传
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "brand-logo-upload")
	public void goods_pics_upload(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String path = ImageUtils.getServletWebAppsPath(request) + "/brands/";
		String fileName = DateUtil.nowTimeToString2() + "_" + file.getOriginalFilename();
		// System.out.println(path);
		File targetFile = new File(path, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		// 保存
		try {
			file.transferTo(targetFile);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put("datas", ImageUtils.getDataBaseImagePath() + "/brands/" + fileName);
			map.put(Utils.MESSAGE, "上传成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "上传失败！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
}
